package org.light.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class ChangeMyPasswordUser extends Verb implements EasyUIPositions {
	
	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}

	public ChangeMyPasswordUser(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.dbType = domain.getDbType();
		this.setVerbName("ChangeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+ StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("修改自己密码");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("Change My Password");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName());
			method.setReturnType(new Type("Map<String,Object>"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+ ".shiro.UserRegisteAndLogin");
			method.addAdditionalImport("org.apache.shiro.SecurityUtils");
			method.addAdditionalImport("org.apache.shiro.session.Session");
			method.addSignature(new Signature(1,"old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName(),"String"));
			method.addSignature(new Signature(2,this.domain.findFieldByFixedName("password").getLowerFirstFieldName(),"String"));
			method.addMetaData("RequestMapping(value = \"/changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"\", method = RequestMethod.POST)");
	
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(2000L,2,"Session session  = SecurityUtils.getSubject().getSession();"));
			sList.add(new Statement(3000L,2,""+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+"0 = ("+this.domain.getCapFirstDomainNameWithSuffix()+")session.getAttribute(\"user\");"));
			sList.add(new Statement(4000L,2,"if("+this.domain.getLowerFirstDomainName()+"0!=null&&!StringUtil.isBlank("+this.domain.getLowerFirstDomainName()+"0.get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"())) {"));
			GetSaltUser getSalt = new GetSaltUser(this.domain);
			GetPasswordUser getPassword = new GetPasswordUser(this.domain);
			FindByName find = new FindByName(this.domain);
			ChangePasswordUser changePassword = new ChangePasswordUser(this.domain);
			sList.add(new Statement(5000L,2,"String old"+this.domain.findFieldByFixedName("salt").getCapFirstFieldName()+" = service."+StringUtil.lowerFirst(getSalt.getVerbName())+"("+this.domain.getLowerFirstDomainName()+"0.get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"());"));
			sList.add(new Statement(6000L,2,"String verfy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = UserRegisteAndLogin.getInputPasswordCiph(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+",old"+this.domain.findFieldByFixedName("salt").getCapFirstFieldName()+");"));
			sList.add(new Statement(7000L,2,"String dbold"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = service."+StringUtil.lowerFirst(getPassword.getVerbName())+"("+this.domain.getLowerFirstDomainName()+"0.get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"());"));
			sList.add(new Statement(8000L,2,"if (verfy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+".equals(dbold"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+")) {"));
			sList.add(new Statement(9000L,3,""+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" = service."+StringUtil.lowerFirst(find.getVerbName())+"("+this.domain.getLowerFirstDomainName()+"0.get"+this.domain.findFieldByFixedName("userName").getCapFirstFieldName()+"());"));
			sList.add(new Statement(10000L,0,""));
			sList.add(new Statement(11000L,3,"String[] saltAndCiphertext = UserRegisteAndLogin.encryptPassword("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+");"));
			sList.add(new Statement(12000L,3,""+this.domain.getLowerFirstDomainName()+".set"+this.domain.findFieldByFixedName("loginFailure").getCapFirstFieldName()+"(0);"));
			sList.add(new Statement(13000L,3,""+this.domain.getLowerFirstDomainName()+".set"+this.domain.findFieldByFixedName("salt").getCapFirstFieldName()+"(saltAndCiphertext[0]);"));
			sList.add(new Statement(14000L,3,""+this.domain.getLowerFirstDomainName()+".set"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"(saltAndCiphertext[1]);"));
			sList.add(new Statement(15000L,0,""));
			sList.add(new Statement(16000L,3,"service."+StringUtil.lowerFirst(changePassword.getVerbName())+"("+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(17000L,3,"result.put(\"success\",true);"));
			sList.add(new Statement(18000L,3,"result.put(\"data\",null);"));
			sList.add(new Statement(19000L,0,""));
			sList.add(new Statement(20000L,3,"return result;"));
			sList.add(new Statement(21000L,3,"}"));
			sList.add(new Statement(22000L,2,"}"));
			sList.add(new Statement(23000L,2,"result.put(\"success\",false);"));
			sList.add(new Statement(24000L,2,"result.put(\"data\",null);"));
			sList.add(new Statement(25000L,2,"return result;"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		if (this.denied) return null;
		else {
			JavascriptBlock block = new JavascriptBlock();
			block.setSerial(100);
			block.setStandardName("changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+domain.getCapFirstDomainName());
			StatementList sList = new StatementList();

			sList.add(new Statement(1000L,0,"{"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(2000L,1,"text:'Change Password',"));
			}else {
				sList.add(new Statement(2000L,1,"text:'设置密码',"));	
			}			
			sList.add(new Statement(3000L,1,"iconCls:'icon-reload',"));
			sList.add(new Statement(4000L,1,"handler:function(){"));
			sList.add(new Statement(5000L,2,"var rows = $(\"#dg\").datagrid(\"getChecked\");"));
			sList.add(new Statement(6000L,2,"if (rows == undefined || rows == null || rows.length == 0 ){"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(7000L,3,"$.messager.alert(\"Warning\",\"Please select one record.\",\"warning\");"));
			}else {
				sList.add(new Statement(7000L,3,"$.messager.alert(\"警告\",\"请选定一条记录！\",\"warning\");"));
			}
			sList.add(new Statement(8000L,3,"return;"));
			sList.add(new Statement(9000L,2,"}"));
			sList.add(new Statement(10000L,2,"if (rows.length > 1) {"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(11000L,3,"$.messager.alert(\"Warning\",\"Please select one record.\",\"warning\");"));
			}else {
				sList.add(new Statement(11000L,3,"$.messager.alert(\"警告\",\"请选定一条记录！\",\"warning\");"));
			}
			sList.add(new Statement(12000L,3,"return;"));
			sList.add(new Statement(13000L,2,"}"));
			sList.add(new Statement(14000L,2,"$(\"#ffchangePassword\").find(\"#"+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+"\").val(rows[0][\""+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+"\"]);"));
			sList.add(new Statement(15000L,2,"$('#wchangePassword').window('open');"));
			sList.add(new Statement(16000L,1,"}"));
			sList.add(new Statement(17000L,0,"}"));
			block.setMethodStatementList(sList);
			return block;			
		}
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		if (this.denied) return null;
		else {
			Domain domain = this.domain;
			JavascriptMethod method = new JavascriptMethod();
			method.setSerial(200);
			method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));			

			List<Writeable> sList = new ArrayList<Writeable>();

			sList.add(new Statement(1000L,1,"var "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" = $(\"#ffchangePassword\").find(\"#"+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"\").val();"));
			sList.add(new Statement(2000L,1,"var old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = $(\"#ffchangePassword\").find(\"#old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"\").val();"));
			sList.add(new Statement(3000L,1,"var confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = $(\"#ffchangePassword\").find(\"#confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"\").val();"));
			sList.add(new Statement(4000L,1,"if (isBlank(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+")){"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(5000L,2,"$.messager.alert(\"Error\",\"Old password can not be empty.\",\"error\");"));
				sList.add(new Statement(6000L,2,"return;"));
				sList.add(new Statement(7000L,1,"}"));
				sList.add(new Statement(8000L,1,"if (isBlank("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+")||isBlank(confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+")){"));
				sList.add(new Statement(9000L,2,"$.messager.alert(\"Error\",\"New password can not be empty.\",\"error\");"));
				sList.add(new Statement(10000L,2,"return;"));
				sList.add(new Statement(11000L,1,"}"));
				sList.add(new Statement(12000L,1,"if (old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" == "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"){"));
				sList.add(new Statement(13000L,2,"$.messager.alert(\"Error\",\"New and old passwords can not be same.\",\"error\");"));
				sList.add(new Statement(14000L,2,"return;"));
				sList.add(new Statement(15000L,1,"}"));
				sList.add(new Statement(16000L,1,"if ("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"!=confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"){"));
				sList.add(new Statement(17000L,2,"$.messager.alert(\"Error\",\"New and confirm new password does not match.\",\"error\");"));
			} else {
				sList.add(new Statement(5000L,2,"$.messager.alert(\"错误\",\"旧密码不可为空！\",\"error\");"));
				sList.add(new Statement(6000L,2,"return;"));
				sList.add(new Statement(7000L,1,"}"));
				sList.add(new Statement(8000L,1,"if (isBlank("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+")||isBlank(confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+")){"));
				sList.add(new Statement(9000L,2,"$.messager.alert(\"错误\",\"新密码不可为空！\",\"error\");"));
				sList.add(new Statement(10000L,2,"return;"));
				sList.add(new Statement(11000L,1,"}"));
				sList.add(new Statement(12000L,1,"if (old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" == "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"){"));
				sList.add(new Statement(13000L,2,"$.messager.alert(\"错误\",\"新密码和旧密码不可相同！\",\"error\");"));
				sList.add(new Statement(14000L,2,"return;"));
				sList.add(new Statement(15000L,1,"}"));
				sList.add(new Statement(16000L,1,"if ("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"!=confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"){"));
				sList.add(new Statement(17000L,2,"$.messager.alert(\"错误\",\"新密码不匹配！\",\"error\");"));
			}
			sList.add(new Statement(18000L,2,"return;"));
			sList.add(new Statement(19000L,1,"}"));
			sList.add(new Statement(20000L,1,"$.ajax({"));
			sList.add(new Statement(21000L,2,"type: \"post\","));
			sList.add(new Statement(22000L,2,"url: \"../"+ this.domain.getControllerPackagePrefix()+"profile"+this.domain.getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.getVerbName())+"\","));
			sList.add(new Statement(23000L,2,"data:  {"));
			sList.add(new Statement(24000L,3,this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+":hex_sha1("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"),"));
			sList.add(new Statement(25000L,3,"old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+":hex_sha1(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"),"));
			sList.add(new Statement(26000L,2,"},"));
			sList.add(new Statement(27000L,2,"dataType: 'json',"));
			sList.add(new Statement(28000L,2,"success: function(data, textStatus) {"));
			sList.add(new Statement(29000L,3,"if (data.success) {"));
			sList.add(new Statement(30000L,4,"$(\"#wchangePassword\").window(\"close\");"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(31000L,4,"$.messager.alert(\"Success\",\"Successfully changed password.\",\"info\");"));
				sList.add(new Statement(32000L,3,"} else if (!data.success) {"));
				sList.add(new Statement(33000L,4,"$(\"#wchangePassword\").window(\"close\");"));
				sList.add(new Statement(34000L,4,"$.messager.alert(\"Failure\",\"Change password failed.\",\"error\");"));
			} else {
				sList.add(new Statement(31000L,4,"$.messager.alert(\"成功\",\"成功修改密码！\",\"info\");"));
				sList.add(new Statement(32000L,3,"} else if (!data.success) {"));
				sList.add(new Statement(33000L,4,"$(\"#wchangePassword\").window(\"close\");"));
				sList.add(new Statement(34000L,4,"$.messager.alert(\"失败\",\"修改密码失败！\",\"error\");"));
			}
			sList.add(new Statement(35000L,3,"}"));
			sList.add(new Statement(36000L,2,"},"));
			sList.add(new Statement(37000L,2,"complete : function(XMLHttpRequest, textStatus) {"));
			sList.add(new Statement(38000L,2,"},"));
			sList.add(new Statement(39000L,2,"error : function(XMLHttpRequest,textStatus,errorThrown) {"));
			sList.add(new Statement(40000L,3,"alert(\"Error:\"+textStatus);"));
			sList.add(new Statement(41000L,3,"alert(errorThrown.toString());"));
			sList.add(new Statement(42000L,2,"}"));
			sList.add(new Statement(43000L,1,"});"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;	
		}
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}
}
